package com.zxl.designpattern.behavioral.command;

/**
 * 命令模式测试类
 * 遥控器是调用者，持有多个命令对象
 * 命令对象实现命令接口，并且持有接收者对象
 * 电视、电灯是接收者，执行命令功能
 */
public class Test {
    public static void main(String[] args) {
        // 创建遥控器
        RemoteControl remoteControl = getRemoteControl();

        // 显示遥控器信息
        System.out.println(remoteControl);

        // 使用遥控器控制家电
        System.out.println("--- 按下遥控器按钮 ---");
        remoteControl.onButtonWasPushed(0);
        remoteControl.offButtonWasPushed(0);
        remoteControl.onButtonWasPushed(1);
        remoteControl.offButtonWasPushed(1);
        remoteControl.onButtonWasPushed(2);
        remoteControl.offButtonWasPushed(2);
        remoteControl.onButtonWasPushed(3);
        remoteControl.offButtonWasPushed(3);

        // 使用撤销功能
        System.out.println("\n--- 使用撤销功能 ---");
        remoteControl.undoButtonWasPushed();
        remoteControl.undoButtonWasPushed();
        remoteControl.undoButtonWasPushed();
        remoteControl.undoButtonWasPushed();
    }

    private static RemoteControl getRemoteControl() {
        RemoteControl remoteControl = new RemoteControl();

        // 创建家电设备
        Light livingRoomLight = new Light("客厅");
        Light kitchenLight = new Light("厨房");
        TV tv = new TV("客厅");

        // 创建家电控制命令
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
        LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);
        TVOnCommand tvOn = new TVOnCommand(tv);
        TVOffCommand tvOff = new TVOffCommand(tv);

        // 将命令设置到遥控器插槽
        remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
        remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
        remoteControl.setCommand(2, tvOn, tvOff);
        return remoteControl;
    }
}    